perm filename LECT7.TEX[ARK,TEX] blob
sn#769046 filedate 1984-08-21 generic text, type C, neo UTF8
COMMENT ā VALID 00007 PAGES
C REC PAGE DESCRIPTION
C00001 00001
C00002 00002 % copyright 1984 by Arthur Keller ... All rights reserved
C00003 00003 \section{Boxes \& Glue}
C00012 00004 \section{Modes}
C00017 00005 \section{Infinite Glue}
C00021 00006 \section{Text Which Sticks Out of Boxes}
C00026 00007 \section*{Assignment}
C00028 ENDMK
Cā;
% copyright 1984 by Arthur Keller ... All rights reserved
\chapter{\TeX\ Fundamentals 1}
%Tue, August 21
%2-3pm
%TeX Fundamentals 1
%>Boxes and glue
%>Modes
%>Penalties
%3-5pm
%<lab assignment 5>
%This input file copyright (c) 1984 Arthur Keller
%Need to put copyright notice in the output?
The purpose of this lecture is to learn more about some \TeX\ fundamentals,
especially boxes and glue, and the various ``modes'' in which \TeX\ operates.
\section{Boxes \& Glue}
We have seen that one runs \TeX\ by using a text editor to create a file
with extension {\tt .tex}, and then running \TeX\ on that file.
The resulting {\tt .dvi} file is then turned into a printable file by a
device driver, and sent to the printer.
It turns out that \TeX\ does not really know, and does not need to know,
the actual shapes of characters to be printed.
What \TeX\ does know is the dimensions of the box which bound or contain
each character, and how those boxes need to line up in relation to one
another.
This information is contained in a file known as the {\tt TFM}, or {\sl \TeX\
Font Metric}, which exists for each font.
The output driver, on the other hand, must know the actual image of the
characters to be printed, which are contained in a series of files containing
{\sl rasters\/}; they define a pattern of dots (several hundred to the
inch) which when printed produce the desired character.
A character box is characterized by three dimensions:
its height, which is the distance from the baseline to the top of the box;
its depth, which is the distance from the baseline to the bottom of the
box; and its width.
There is actually a little space around each character inside its box, so
that when boxes are placed adjacent to each other, the characters are not
actually touching.
(This space around a character is known as its {\sl sidebar}.)
Each character box also has a {\sl reference point} on its left margin at
the baseline (the line on which the characters rest), which is how the
character boxes are lined up.
We know that, unlike a typewriter, each character does not necessarily
have the same width to \TeX\ (except of course in the \cmd{tt} typeface).
However, there is another way in which \TeX\ differs from a typewriter, as
well:
The size of the space {\sl between} words or lines is also variable, which
is what enables \TeX\ to justify lines of output.
The space between boxes (either words or lines) in \TeX\ is known as glue.
It has the property of being able to stretch or shrink while smaller boxes
are being assembled into larger boxes, but then becomes fixed thereafter.
Each component of glue is characterized by three attributes: its natural
width, or {\sl space}, its {\sl stretch}, and its {\sl shrink}.
Stretch and shrink define how much larger or smaller \TeX\ can make a
given element of glue in order to achieve the desired result.
Consider the example on page 69 of the \TeX book, which shows a line composed
of four boxes and the space between them. The boxes have widths of 5, 6,
3, and 8, for a total of 22,
while the glue between them can be characterized as follows:
$$\vbox{
\halign{#\hfil\quad&\hfil #\quad\hfil&\hfil #\quad\hfil&\hfil # \hfil\cr
\bf\hfil Glue between&\bf Space&\bf Stretch&\bf Shrink\cr
\noalign{\smallskip}
first and second boxes&9&3&1\cr
second and third boxes&9&6&2\cr
third and fourth boxes&12&0&0\cr
\bf TOTAL&30&9&3\cr}}$$
Thus the {\sl natural width} of the boxes and the glue between them is
equal to 52 (the sum of the box widths and the natural spaces of the glue
elements), with a total of 9 units of stretch and 3 units of shrink.
If we were to put this line in an \cmd{hbox} without specifying another
\cmd{hsize}, then it would be an \cmd{hbox} to the natural size of the
line, or 52~points.
Suppose, however, that we wanted to put this line in an \cmd{hbox to
58pt}, or 6 points greater than the natural width.
Then \TeX\ will distribute the 6 units of extra space required evenly over
the 9 units of stretchability present, by multiplying each unit of stretch
present by $6/9$.
Thus the space between the first and second boxes will be $9 + 2 = 11$,
between the second and third $9 + 4 = 13$, and between the third and
fourth 12 (since there was no stretch present).
The total line will have a width of 22 (the boxes) plus 36 (the glue) for
58 points total.
The extra space is thus spread proportionately over the stretch components
of the glue present.
If we wanted an \cmd{hbox to 49pt}, then \TeX\ would need to reduce the
natural width of the line by 3 points, which would require using up all of
the shrink capacity present, leaving the three pieces of glue of 8, 7, and
12 points each.
What if we specified an \cmd{hbox to 46pt}?
Then we would be asking \TeX\ to shrink the line by more than the 3 points
of shrink available, so an {\sl overfull box} error message will result,
warning us that the line sticking out the edge of the box that we asked
\TeX\ to place it in.
On the other hand, if we said \cmd{hbox to 64pt}, asking for 12 points of
stretch, or 3 more than we have present, \TeX\ will give us an {\sl underfull
box} warning, but then will go ahead and distribute the 12 points of
extra space over the line, by multiplying each unit of stretch present by
12/9.
In other words, in this case \TeX\ warns us, but then goes and does the
best that it can.
It is even possible to get \TeX\ to stretch or shrink the contents of a
line without having to know its natural width.
We do this by using commands like \cmd{hbox spread 12pt}, which puts the
line in a box 12 points wider than its natural width, or \cmd{hbox spread
-3pt}, to make something 3 points smaller.
This is useful for making a title slightly wider than it would be for
emphasis, or for fitting things where they otherwise would not quite fit.
\section{Modes}
\TeX\ has six modes.
It is not absolutely necessary to understand the modes in order to use
\TeX, but is turns out to be quite helpful in understanding why you cannot
do what you are trying to do when you get an error message.
The six modes are as follows:
\begin{itemize}
\item Horizontal Mode: while \TeX\ is constructing an ordinary paragraph out
of text and glue
\item Vertical Mode: while \TeX\ is in between paragraphs or adding
vertical glue
\item Math Mode: while writing math formulas; delimited by \$ before and after
\item Restricted Horizontal Mode: inside an \cmd{hbox}; useful to get ordinary
text inside a vertical box or math mode, and to prevent line breaks
\item Restricted Vertical Mode: inside a \cmd{vbox}; can be used to put things
like \cmd{halign} inside math mode
\item Display Math Mode: displaying formulas; delimited by \$\$ before and
after
\end{itemize}
Note that there is a symmetry here:
each of the last three modes is quite similar to one of the first
three.
Thus, \TeX\ is in horizontal mode while inside of a paragraph.
Once \TeX\ encounters the end of the paragraph (either two consecutive
blank lines or a \cmd{par} command) or any vertical glue (a \cmd{bigskip},
for example), it shifts into vertical mode.
\TeX\ stays in vertical mode until it sees the beginning of the next
paragraph, or a \cmd{indent} or \cmd{noindent} command.
Note that if the first thing in a paragraph is a mathematical formula,
\TeX\ will automatically shift into horizontal mode {\sl before} entering
math mode, and will shift back to horizontal mode after math mode is
exited.
One way of understanding the difference between horizontal and vertical
modes is to see how \TeX\ handles the commands \cmd{hbox\lb A\rb\bs
hbox\lb B\rb} in the two modes.
If we given these commands while in horizontal mode, \TeX\ will treat them
as two boxes to be placed on a horizontal line, as follows:
\hbox{A}\hbox{B}.
If, on the other hand, these commands were given while \TeX\ was in
vertical mode, the following would result: \par\hbox{A}\hbox{B}
\noindent Why?
Because \TeX\ was in vertical mode, it treated the two \cmd{hbox}es as two
lines of text, rather than as two boxes to be placed horizontally
adjacent.
Thus, one should never begin a paragraph with an \cmd{hbox} command, or
strange things are likely to happen.
To get around this problem, begin the paragraph with \cmd{noindent} (which
begins horizontal mode), or give the command \cmd{leavevmode} (which
places \TeX\ in horizontal mode if it was not already, and does nothing if
it was).
\section{Infinite Glue}
In addition to the ordinary glue that we discussed above, \TeX\ has some
glue which has a natural width of zero, but an infinite amount of stretch
and/or shrink.
These types of glue are very useful in creating text which is flush left
or right or which is centered, depending on what combinations of commands
are given.
\begin{figure}[p]
\vskip6truein
\caption{Put intro2.tex here}
\end{figure}
\begin{figure}[p]
\vskip6truein
\caption{Put intro2.dvi here}
\end{figure}
In fact, \TeX\ has three orders of glue with infinite stretchability:
the smallest is the {\tt fil}, the next largest is the {\tt fill}, and the
largest is the {\tt filll}.
Corresponding to these are the horizontal spaces \cmd{hfil} and
\cmd{hfill} and the vertical spaces \cmd{vfil} and \cmd{vfill}.
(There are no \cmd{hfilll} or \cmd{vfilll}, since use of the highest order
of infinity is discouraged.)
\TeX\ also has commands \cmd{hss} and \cmd{vss} for glue which is
infinitely shrinkable as well as stretchable; the {\tt ss} stands for
{\sl stretch or shrink}.
In outputting text, \TeX\ sets any ordinary glue first, and then fills the
remaining space by any infinite glue which is present.
However, only the infinite glue of the highest order counts; any lower
order glue is simply ignored.
Let us look at some examples of how these infinite pieces of glue work.
Suppose we give the command \cmd{hbox to \bs hsize\lb\bs hfil Text to be
centered\bs hfil\rb}.
Then the result will be:
\smallskip
\hbox to \hsize{\hfil Text to be centered\hfil}
\smallskip
\noindent and in fact this is very similar to how the command
\cmd{centerline} is defined (although it has \cmd{hss} instead of
\cmd{hfil}).
If we said \cmd{hbox to \bs hsize\lb\bs hfil\bs hfil Text to be
centered\bs hfil\rb}, then two-thirds of the space will occur before the
text, and one third after, since there are two \cmd{hfil}s before and one
after.
On the other hand, if we said \cmd{hbox to \bs hsize\lb\bs hfil\bs hfil
Text to be centered\bs hfill\rb}, then the text will be set flush left,
since the one \cmd{hfill} will completely dominate the \cmd{hfil}s.
\section{Text Which Sticks Out of Boxes}
The command \cmd{hskip} followed by a distance causes \TeX\ to skip that
amount of horizontal space before printing the next character.
The space can be negative (i. e., causing \TeX\ to backspace) as well as
positive.
For example, if we give the commands {\tt Left\bs hskip 2in Right\bs
hskip -1.5in Middle}, then our output will be as follows:
\smallskip
\hbox to \hsize{Left\hskip 2in Right \hskip -1.5in Middle\hfil}
\smallskip
Note that after this sequence of commands, the right side of the current box
is after the word ``Middle'' even though it appears to the left of the word
``Right''. If additional text followed these commands, that text would
overprint the word ``Right'', which is in effect hanging out of the end of
the box.
Having text stick out of a box is occasionally useful, and in fact there
are two commands whose result is just that, \cmd{llap} and \cmd{rlap}.
The macro \cmd{llap} causes text to stick out the left side of a box, while
\cmd{rlap} does the same thing on the right side.
For example, if we wanted to have an indented paragraph preceded by a
bullet, instead of using the \cmd{bullpar} macro we created earlier, we
could simply say \cmd{leavevmode\bs llap\lb\$\bs bullet\rb} which would
have the same effect.
How does a macro like \cmd{llap} or \cmd{rlap} work?
This is somewhat easier to see if we begin by looking at \cmd{rlap}.
According to the \TeX book (p.~82), \cmd{rlap} is defined as \cmd{hbox to
0pt\lb\#1\bs hss\rb}.
Since \cmd{hss} gives us infinite shrinkability, the total width of the
\cmd{hbox} can be zero, as required, even though the contents have a
positive width.
In other words, \TeX\ starts at a given point on the page, outputs the
contents of the \cmd{rlap}, but then backs up by the width of those
contents, returning us to the point at which we started.
Normally we think of glue as stretching to fill a box of the specified
width; however, here the glue is shrinking to allow the total box to
occupy width zero.
The macro \cmd{llap} is defined similarly, except that the glue appears
before the contents: \cmd{def\bs llap\#1\lb\bs hbox to 0pt\lb\bs hss
\#1\rb\rb}.
Thus, the contents of the box are pushed to the left by the presence of
the \cmd{hss}, and appear to the left of the normal \cmd{hbox} enclosing
each line.
If we wanted to get space, say 5 points worth, between the contents of the
box and the rest of our text, rather than specifying \cmd{hss} we could
ask for \cmd{llap\lb Contents of the Box\bs hskip 5pt\rb}, where the space
would push the Contents out into the margin by the required distance.
Note that the overall width of the box is still zero.
In defining the macros for \cmd{llap} and \cmd{rlap}, it is very important
not to allow spaces between the parameters and the brackets; otherwise
unwanted spaces in the output will result.
In general, one should be very caseful aboout spaces in commands which do
things in horizontal mode, for just this reason.
\section*{Assignment}
Assignment for this lecture:
Typeset the following.
(The first four lines have lengths that increase 5~points at a time.)
\hbox{\qquad ``Oh, oh!'' cried Baby Sally. Dick and Jane laughed.}
\hbox spread 5pt{\qquad ``Oh, oh!'' cried Baby Sally. Dick and Jane laughed.}
\hbox spread 10pt{\qquad ``Oh, oh!'' cried Baby Sally. Dick and Jane laughed.}
\hbox spread 15pt{\qquad ``Oh, oh!'' cried Baby Sally. Dick and Jane laughed.}
\medskip
\hbox to \hsize{This line\quad(yawn)\quad really\hfil stretches \hfil the full width.}
\medskip
\hbox to \hsize{\rlap{This is really wide}\hfil Center!\hfil\llap{short}}
\bigskip
(Notice that ``Center!'' is centered on the line independently of
the text on both sides.)
\medskip
Figure out how to get the AMS-TeX logo on one line when it starts
a paragraph.